﻿using MongoDB.Bson;
using MongoDB.Driver.Builders;
using SmallJob.Model;
using SmallShop.Services.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;

namespace SmallShop.Services.Implements
{
    public class MConversationService : BaseMongoService<MConversation>, IMConversationService
    {
        public MConversationService()
            : base()
        {
        }

        protected override MongoDB.Driver.MongoCollection<MConversation> GetCollection()
        {
            return db.GetCollection<MConversation>("conversations");
        }

        public override void Add(MConversation cvs)
        {
            if (cvs.Messages != null) cvs.Messages = null;
            base.Add(cvs);
        }

        public List<MConversation> GetConversationByID(int userid)
        {
            var lsttype = new List<int>();
            lsttype.Add((int)MessageType.User_User);
            lsttype.Add((int)MessageType.PROVIDE);
            lsttype.Add((int)MessageType.REQUEST);
            var query = Query.Or(
                    Query.And(
                        Query.EQ("Person1ID", userid),
                        Query.Or(
                             Query.In("Type", new BsonArray(lsttype))
                        )
                    ),
                    Query.And(
                     Query.EQ("Person2ID", userid),
                        Query.Or(
                             Query.In("Type", new BsonArray(lsttype))
                        )
                    )
                );
            var lsttemt = GetCollection().Find(query);
            return lsttemt.OrderByDescending(c => c.Message.Date).ToList();
        }

        public MConversation GetConversationByShortID(String shortid)
        {
            var query = Query<MConversation>.EQ(c => c.ShortID, shortid);
            return GetCollection().FindOne(query);
        }

        public List<MConversation> GetConversationOrderByID(int userid)
        {
            var lsttype = new List<int>();
            lsttype.Add((int)MessageType.ORDER_PROVIDE);
            lsttype.Add((int)MessageType.ORDER_REQUEST);
            var query = Query.Or(
                    Query.And(
                        Query.EQ("Person1ID", userid),
                        Query.In("Type", new BsonArray(lsttype))
                    ),
                    Query.And(
                        Query.EQ("Person2ID", userid),
                        Query.In("Type", new BsonArray(lsttype))
                    )
                );
            var lsttemt = GetCollection().Find(query);
            return lsttemt.Where(c => !c.Order.IsNoDisplay).OrderByDescending(c => c.Message.Date).ToList();
        }

        public void UpdateLastMessage(String cvsid, MMessage mes)
        {
            var query = Query<MConversation>.EQ(c => c.ShortID, cvsid);
            var update = Update<MConversation>.Set(c => c.Message, mes);
            GetCollection().Update(query, update);
        }

        public List<MConversation> GetConversationByPage(int page, int pagesize, out long total, int userid, int type)
        {
            var query = Query.And(
                    Query.Or(
                           Query<MConversation>.EQ(c => c.Person1ID, userid),
                          Query<MConversation>.EQ(c => c.Person2ID, userid)
                    ),
                    Query<MConversation>.EQ(c => c.Type, type),
                    Query.Or(
                        Query<MConversation>.EQ(c => c.Order, null),
                        Query.And(
                              Query.Not(Query<MConversation>.EQ(c => c.Order, null)),
                              Query<MConversation>.EQ(c => c.Order.IsNoDisplay, new BsonBoolean(false))
                        )
                    )

                );
            int skip = (page - 1) * pagesize;
            var cursor = GetCollection().Find(query);
            cursor.SetSortOrder(SortBy<MConversation>.Descending(c => c.Message.Date));
            cursor.SetSkip(skip);
            cursor.SetLimit(pagesize);
            total = cursor.Count();
            var lst = cursor.ToList();
            return lst;
        }

        public List<MConversation> GetConversationOrderByPage(int page, int pagesize, out long total, int userid, bool mysell)
        {
            var prequery = Query<MConversation>.EQ(c => c.Order.SellerID, userid);
            if (!mysell) prequery = Query.Not(Query<MConversation>.EQ(c => c.Order.SellerID, userid));
            var query = Query.And(
                    Query.Or(
                           Query<MConversation>.EQ(c => c.Person1ID, userid),
                          Query<MConversation>.EQ(c => c.Person2ID, userid)
                    ),
                    Query.Not(Query<MConversation>.EQ(c => c.Order, null)),
                    Query<MConversation>.EQ(c => c.Order.IsNoDisplay, new BsonBoolean(false)),
                    prequery
                );
            int skip = (page - 1) * pagesize;
            var cursor = GetCollection().Find(query);
            cursor.SetSortOrder(SortBy<MConversation>.Descending(c => c.Message.Date));
            cursor.SetSkip(skip);
            cursor.SetLimit(pagesize);
            total = cursor.Count();
            var lst = cursor.ToList();
            return lst;
        }

        public void MakeCvsNoDisplay(String shortid)
        {
            var query = Query<MConversation>.EQ(c => c.ShortID, shortid);
            var update = Update<MConversation>.Set(c => c.Order.IsNoDisplay, new BsonBoolean(true));
            GetCollection().Update(query, update);
        }
    }
}